home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Tools / ResAnomaly 1.2 ƒ / ResAnomaly Source / LTempHandleStream.cp < prev    next >
Text File  |  1995-08-04  |  3KB  |  152 lines

  1. // ===========================================================================
  2. //    LHandleStream.cp                ©1993 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    A Stream whose bytes are in a Handle block in memory
  6. // * Note: ckt modified to use temp. memory!  Don't know why there isn't
  7. // * such a variant included with PP.
  8.  
  9. #ifdef PowerPlant_PCH
  10. #include PowerPlant_PCH
  11. #endif
  12.  
  13. #include <LHandleStream.h>
  14.  
  15.  
  16. // ---------------------------------------------------------------------------
  17. //        • LHandleStream
  18. // ---------------------------------------------------------------------------
  19. //    Default Constructor
  20.  
  21. LHandleStream::LHandleStream()
  22. {
  23.     OSErr    theErr;
  24.     
  25.     mDataH = ::TempNewHandle(0, &theErr);            // Start with a zero length Handle
  26.     // * purposely ignoring theErr
  27.     
  28.     ThrowIfMemFail_(mDataH);
  29. }
  30.  
  31.  
  32. // ---------------------------------------------------------------------------
  33. //        • LHandleStream(Handle)
  34. // ---------------------------------------------------------------------------
  35. //    Construct from an existing Handle
  36. //
  37. //    The LHandleStream object assumes control of the Handle
  38.  
  39. LHandleStream::LHandleStream(
  40.     Handle    inHandle)
  41. {
  42.     mDataH = inHandle;
  43.     LStream::SetLength(GetHandleSize(inHandle));
  44. }
  45.  
  46.  
  47. // ---------------------------------------------------------------------------
  48. //        • ~LHandleStream
  49. // ---------------------------------------------------------------------------
  50. //    Destructor
  51.  
  52. LHandleStream::~LHandleStream()
  53. {
  54.     if (mDataH != nil) {
  55.         ::DisposeHandle(mDataH);
  56.     }
  57. }
  58.  
  59.  
  60. void
  61. LHandleStream::SetLength(
  62.     Int32    inLength)
  63. {
  64.     ::SetHandleSize(mDataH, inLength);
  65.     ThrowIfMemError_();
  66.     LStream::SetLength(inLength);
  67. }
  68.  
  69.  
  70. Int32
  71. LHandleStream::WriteData(
  72.     const void    *inBuffer,
  73.     Int32        inByteCount)
  74. {
  75.     Int32    bytesWritten = inByteCount;
  76.     Int32    endOfWrite = GetMarker() + inByteCount;
  77.     if (endOfWrite > GetLength()) {
  78.                                     // Need to grow Handle
  79.         Try_ {
  80.             SetLength(endOfWrite);
  81.         }
  82.         
  83.         Catch_(inErr) {                // Grow failed. Write only what fits.
  84.             bytesWritten = GetLength() - GetMarker();
  85.         } EndCatch_
  86.     }
  87.  
  88.                                     // Copy bytes into Handle
  89.     ::BlockMoveData(inBuffer, *mDataH + GetMarker(), bytesWritten);
  90.     SetMarker(bytesWritten, streamFrom_Marker);
  91.     return bytesWritten;
  92. }
  93.  
  94.  
  95. Int32
  96. LHandleStream::ReadData(
  97.     void    *outBuffer,
  98.     Int32    inByteCount)
  99. {
  100.                                     // Upper bound is number of bytes from
  101.                                     //   marker to end
  102.     Int32    bytesRead = inByteCount;
  103.     if ((GetMarker() + bytesRead) > GetLength()) {
  104.         bytesRead = GetLength() - GetMarker();
  105.     }
  106.                                     // Copy bytes from Handle into buffer
  107.     ::BlockMoveData(*mDataH + GetMarker(), outBuffer, bytesRead);
  108.     SetMarker(bytesRead, streamFrom_Marker);
  109.  
  110.     return bytesRead;
  111. }
  112.  
  113.  
  114. void
  115. LHandleStream::SetDataHandle(
  116.     Handle    inHandle)
  117. {
  118.     if (mDataH != nil) {
  119.         ::DisposeHandle(mDataH);
  120.     }
  121.         
  122.     mDataH = inHandle;
  123.     
  124.     Int32    newLength = 0;
  125.     if (inHandle != nil) {
  126.         newLength = GetHandleSize(inHandle);
  127.     }
  128.     LStream::SetLength(newLength);
  129.         
  130.     SetMarker(0, streamFrom_Start);
  131. }
  132.  
  133.  
  134. Handle
  135. LHandleStream::GetDataHandle()
  136. {
  137.     return mDataH;
  138. }
  139.  
  140.  
  141. Handle
  142. LHandleStream::DetachDataHandle()
  143. {
  144.     Handle    dataHandle = mDataH;    // Save current data Handle
  145.     
  146.     LStream::SetLength(0);
  147.     mDataH = ::NewHandle(0);        // Reset to zero-sized Handle
  148.     ThrowIfMemFail_(mDataH);
  149.     
  150.     return dataHandle;
  151. }
  152.